package com.meessiom.train.crm.expense.controller;


import com.meessiom.train.crm.expense.model.Expense;
import com.meessiom.train.crm.expense.service.ExpenseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.nio.charset.StandardCharsets;


import java.util.Date;
import java.util.List;


@CrossOrigin("*")
@RestController
@RequestMapping("/api/v1/expenses")
@Slf4j
public class ExpenseController {

    @Autowired
    private ExpenseService expenseService;


    @GetMapping
    public List<Expense> getAllExpenses(
            @RequestParam(required = false) String expenseName,
            @RequestParam(required = false) String expenseType,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date expenseDate,
            @RequestParam(required = false) String supportName,
            @RequestParam(required = false) String expenseState) {
        return expenseService.getAllExpenses(expenseName, expenseType, expenseDate, supportName, expenseState);
    }

    @PostMapping
    public Expense createExpense(@RequestBody Expense expense ){
        return expenseService.createExpense(expense);
    }


    @DeleteMapping({"{id}"})
    public ResponseEntity<HttpStatus> deleteExpense(@PathVariable long id) {
        this.expenseService.deleteExpense(id);
        return new ResponseEntity(HttpStatus.NO_CONTENT);

    }



    @PutMapping("/{id}")
    public Expense updateExpense(@PathVariable long id, @RequestBody Expense expenseDetails) {
        return expenseService.updateExpense(id, expenseDetails);
    }

    @PostMapping("/import")
    public ResponseEntity<String> importExpenses(@RequestParam("file") MultipartFile file) {
        try {
            String content = new String(file.getBytes(), StandardCharsets.UTF_8);
            expenseService.importExpensesFromCsv(content);
            return ResponseEntity.ok("导入成功");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("导入失败: " + e.getMessage());
        }
    }

    @GetMapping("/export")
    public ResponseEntity<byte[]> exportExpenses(
            @RequestParam(required = false) String expenseName,
            @RequestParam(required = false) String expenseType,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date expenseDate,
            @RequestParam(required = false) String supportName,
            @RequestParam(required = false) String expenseState) {

        String csvContent = expenseService.exportExpensesToCsv(
                expenseName, expenseType, expenseDate, supportName, expenseState);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment; filename=expenses.csv");
        headers.add("Content-Type", "text/csv; charset=utf-8");

        return ResponseEntity
                .ok()
                .headers(headers)
                .body(csvContent.getBytes(StandardCharsets.UTF_8));
    }










}
